// generate the sceneMasker and sceneAxis
// scaleInMap & moveInAxis -> maskInMap & moveInAxis to middle
// while to middle, change the scene -> load new scene(onMLInit)
// invoke onMLInit -> reassign sceneMasker -> maskOutMap & maskOutAxis to sides
// -> scaleOutMap & moveOutAxis
// dispose the sceneMasker and sceneAxis

var sceneMasker:MovieClip;
var themeMasker:MovieClip
var sceneAxis:MovieClip;
var processFlag:Boolean;

var themeBg:MovieClip;
var contentPaper:MovieClip;

var scene:String;

function disposeAll()
{
	sceneMasker.removeMovieClip();
	themeMasker.removeMovieClip();
	sceneAxis.removeMovieClip();

	delete sceneMasker;
	delete themeMasker;
	delete sceneAxis;
	delete themeBg;
	delete contentPaper;

	delete scene;
	processFlag=false;
	_quality="HIGH";

	Packages.system.mlf.GateKeeper.dispatchMessage({type:"onSceneTransitionComplete"});
}

function rollIn(sceneName:String)
{
	_quality="LOW";
	
	//
	if (processFlag) return;
	processFlag=true;

	//
	scene=sceneName;
	
	//
	generateSceneMasker();
	generateSceneAxis();

	//
	var themeBasic=Packages.system.mlf.GateKeeper.scanElement("ThemeBg", "basic");
	themeBg=themeBasic._parent;
	var contentSlot=Packages.components.SceneAgent.componentsViewArr.content;
	contentPaper=Packages.system.mlf.GateKeeper.scanElement(contentSlot, "basic")._parent;

	themeBg.setMask(themeMasker);
	contentPaper.setMask(sceneMasker);

	//
	scaleInMap();
	moveInAxis();
}

function invokeMotionFinished(param)
{
	if (param=="moveInAxis")
	{
		maskInMap();
		moveInAxisMiddle();
	}
	else if (param=="moveInAxisMiddle")
	{
		processFlag=false;
		_quality="HIGH";
		Packages.components.SceneAgent.doLoadPath(scene);
	}
	else if (param=="moveOutAxisMiddle")
	{
		scaleOutMap();
		moveOutAxis();
	}
	else if (param=="moveOutAxis")
	{
		disposeAll();
	}
}

function maskInMap()
{
	Packages.system.render.Animation.move(sceneMasker, "_width", -sceneMasker._width);
	Packages.system.render.Animation.move(sceneMasker, "_x", sceneMasker._width/2);
	Packages.system.render.Animation.move(themeMasker, "_width", -themeMasker._width);
	Packages.system.render.Animation.move(themeMasker, "_x", sceneMasker._width/2);
}

function moveInAxisMiddle()
{
	Packages.system.render.Animation.move(sceneAxis.rightAxis, "_x", -455);
	Packages.system.render.Animation.move(sceneAxis.leftAxis, "_x", 455, this, "moveInAxisMiddle");
}

function moveInAxis()
{
	Packages.system.render.Animation.move(sceneAxis.leftAxis, "_x", 30);
	Packages.system.render.Animation.move(sceneAxis.leftAxis, "_height", -40);
	Packages.system.render.Animation.move(sceneAxis.rightAxis, "_x", -30);
	Packages.system.render.Animation.move(sceneAxis.rightAxis, "_height", -40, this, "moveInAxis");
}

function moveOutAxis()
{
	Packages.system.render.Animation.move(sceneAxis.leftAxis, "_x", -30);
	Packages.system.render.Animation.move(sceneAxis.leftAxis, "_height", 40);
	Packages.system.render.Animation.move(sceneAxis.rightAxis, "_x", 30);
	Packages.system.render.Animation.move(sceneAxis.rightAxis, "_height", 40, this, "moveOutAxis");
}

function scaleInMap()
{
	var typeArr:Array=["_x", "_y", "_height", "_width"];
	var offsetArr:Array=[30, 20, -40, -60];
	Packages.system.render.Animation.moves(themeBg, typeArr, offsetArr);
	Packages.system.render.Animation.moves(contentPaper, typeArr, offsetArr);
}

function scaleOutMap()
{
	var typeArr:Array=["_x", "_y", "_height", "_width"];
	var offsetArr:Array=[-30, -20, 40, 60];
	Packages.system.render.Animation.moves(themeBg, typeArr, offsetArr);
	Packages.system.render.Animation.moves(contentPaper, typeArr, offsetArr);
}

function generateSceneAxis()
{
	sceneAxis=_root.createEmptyMovieClip("sceneAxis", 99);
	var leftAxis=sceneAxis.attachMovie("Axis", "leftAxis", sceneAxis.getNextHighestDepth(), {_x:0, _y:Stage.height/2, _height:Stage.height+40, _width:30});
	var rightAxis=sceneAxis.attachMovie("Axis", "rightAxis", sceneAxis.getNextHighestDepth(), {_x:Stage.width, _y:Stage.height/2, _height:Stage.height+40, _width:30});
}

function generateSceneMasker()
{
	sceneMasker=_root.createEmptyMovieClip("sceneMasker", 97);
	sceneMasker.lineStyle(0, 0x000000);
	sceneMasker.moveTo(0, 0);

	sceneMasker.beginFill(0x000000, 100);
	sceneMasker.lineTo(Stage.width, 0);
	sceneMasker.lineTo(Stage.width, Stage.height);
	sceneMasker.lineTo(0, Stage.height);
	sceneMasker.lineTo(0, 0);
	sceneMasker.endFill();

	themeMasker=_root.createEmptyMovieClip("themeMasker", 98);
	themeMasker.lineStyle(0, 0x000000);
	themeMasker.moveTo(0, 0);

	themeMasker.beginFill(0x000000, 100);
	themeMasker.lineTo(Stage.width, 0);
	themeMasker.lineTo(Stage.width, Stage.height);
	themeMasker.lineTo(0, Stage.height);
	themeMasker.lineTo(0, 0);
	themeMasker.endFill();
}

function rollOut(segmentFlag:Boolean)
{
	_quality="LOW";
	//
	// if (!processFlag) return;
	// processFlag=false;
	if (processFlag) return;
	processFlag=true;

	// 
	if (segmentFlag)
	{
		generateSceneMasker();
		sceneMasker._width=0;
		sceneMasker._x=500;
		themeMasker._width=0;
		themeMasker._x=500;
		sceneAxis=_root.sceneAxis;

		var themeBasic=Packages.system.mlf.GateKeeper.scanElement("ThemeBg", "basic");
		themeBasic._visible=true;
		themeBg=themeBasic._parent;

		// initialize themeBg
		themeBg._x+=30;
		themeBg._y+=20;
		themeBg._width-=60;
		themeBg._height-=40;

		themeBg.setMask(themeMasker);
	}

	//
	var contentSlot=Packages.components.SceneAgent.componentsViewArr.content;
	var basic=Packages.system.mlf.GateKeeper.scanElement(contentSlot, "basic");
	basic._visible=true;
	contentPaper=basic._parent;

	contentPaper.setMask(sceneMasker);

	// initialize contentPaper
	contentPaper._x+=30;
	contentPaper._y+=20;
	contentPaper._width-=60;
	contentPaper._height-=40;

	// maskOutMap & maskOutAxis
	maskOutMap();
	moveOutAxisMiddle();
}

function maskOutMap()
{
	Packages.system.render.Animation.move(sceneMasker, "_width", 940);
	Packages.system.render.Animation.move(sceneMasker, "_x", -470);
	Packages.system.render.Animation.move(themeMasker, "_width", 940);
	Packages.system.render.Animation.move(themeMasker, "_x", -470);
}

function moveOutAxisMiddle()
{
	Packages.system.render.Animation.move(sceneAxis.rightAxis, "_x", 455);
	Packages.system.render.Animation.move(sceneAxis.leftAxis, "_x", -455, this, "moveOutAxisMiddle");
}